package edu.harding.redzone;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class ViewThread extends Thread
{
	private final int OFFENSE = 0;
	private final int DEFENSE = 1;
	private final int PREVIEW = 2;
	private OffenseModeView mOffenseView;
	private DefenseModeView mDefenseView;
	private PreviewModeView mPreviewView;
	private SurfaceHolder mHolder;
	private boolean mRun = false;
	private int mView;
	

	public ViewThread(OffenseModeView view)
	{
		mOffenseView = view;
		mHolder = mOffenseView.getHolder();
		mView = OFFENSE;
	}
	
	public ViewThread(DefenseModeView view)
	{
		mDefenseView = view;
		mHolder = mDefenseView.getHolder();
		mView = DEFENSE;
	}
	
	public ViewThread(PreviewModeView view)
	{
		mPreviewView = view;
		mHolder = mPreviewView.getHolder();
		mView = PREVIEW;
	}

	public void setRunning(boolean run)
	{
		mRun = run;
	}

	@Override
	public void run()
	{
		Canvas canvas = null;
		while (mRun)
		{
			canvas = mHolder.lockCanvas();
			if (canvas != null)
			{
				switch (mView)
				{
				case OFFENSE:
					mOffenseView.doDraw(canvas);
					mHolder.unlockCanvasAndPost(canvas);
					break;
				case DEFENSE:
					mDefenseView.doDraw(canvas);
					mHolder.unlockCanvasAndPost(canvas);
					break;
				case PREVIEW:
					mPreviewView.doDraw(canvas);
					mHolder.unlockCanvasAndPost(canvas);
					break;
				}
				
			}
		}
	}
}